//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "IntegralCdsEngine.h"
using namespace Cephei::QL::Pricingengines::Credit;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::CIntegralCdsEngine (Cephei::QL::Times::IPeriod^ integrationStep, Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ probability, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeSettlementDateFlows) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CPeriod^ _CintegrationStep;
    CDefaultProbabilityTermStructure^ _Cprobability;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
#ifdef HANDLE
        _phIntegralCdsEngine = NULL;
#endif
        _CintegrationStep = safe_cast<CPeriod^> (integrationStep);
        _CintegrationStep->Lock();
        QuantLib::Period& _integrationStep = static_cast<QuantLib::Period&> (_CintegrationStep->GetReference ()); 
        _Cprobability = safe_cast<CDefaultProbabilityTermStructure^> (probability);
        _Cprobability->Lock();
        Handle<QuantLib::DefaultProbabilityTermStructure>& _probability = static_cast<Handle<QuantLib::DefaultProbabilityTermStructure>&> (_Cprobability->GetHandle ()); 
        QuantLib::Real _recoveryRate = (QuantLib::Real)ValueHelper::Convert (recoveryRate);
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        boost::optional<bool> _includeSettlementDateFlows;
        if (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (includeSettlementDateFlows))
        {
            boost::optional<bool> t = boost::optional<bool> (ValueHelper::Convert (includeSettlementDateFlows->Value));
            _includeSettlementDateFlows = boost::optional<boost::optional<bool>> (t);
        }
        else
            _includeSettlementDateFlows = boost::none;
        _ppIntegralCdsEngine = new boost::shared_ptr<QuantLib::IntegralCdsEngine> (new QuantLib::IntegralCdsEngine ( _integrationStep,  _probability,  _recoveryRate,  _discountCurve,  _includeSettlementDateFlows ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CintegrationStep != nullptr) _CintegrationStep->Unlock();
        if (_Cprobability != nullptr) _Cprobability->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::CIntegralCdsEngine (boost::shared_ptr<QuantLib::IntegralCdsEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntegralCdsEngine = NULL;
#endif
	_ppIntegralCdsEngine = &childNative;
    
}
Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::CIntegralCdsEngine (QuantLib::IntegralCdsEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phIntegralCdsEngine = NULL;
#endif
	_ppIntegralCdsEngine = new boost::shared_ptr<QuantLib::IntegralCdsEngine> (&childNative);
    
    _IntegralCdsEngineOwner = owner;
    
}

Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::CIntegralCdsEngine (CIntegralCdsEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntegralCdsEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppIntegralCdsEngine = new boost::shared_ptr<QuantLib::IntegralCdsEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::CIntegralCdsEngine (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntegralCdsEngine = NULL;
#endif
	if (!t->IsSubclassOf(CIntegralCdsEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::CIntegralCdsEngine (QuantLib::Handle<QuantLib::IntegralCdsEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIntegralCdsEngine = &childNative;
	_ppIntegralCdsEngine = &static_cast<boost::shared_ptr<QuantLib::IntegralCdsEngine>>(childNative.currentLink());
    
    _IntegralCdsEngineOwner = owner;
}
Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::CIntegralCdsEngine (QuantLib::Handle<QuantLib::IntegralCdsEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIntegralCdsEngine = &childNative;
	_ppIntegralCdsEngine = &static_cast<boost::shared_ptr<QuantLib::IntegralCdsEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::CIntegralCdsEngine (QuantLib::IntegralCdsEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntegralCdsEngine = NULL;
#endif
	_ppIntegralCdsEngine = new boost::shared_ptr<QuantLib::IntegralCdsEngine> (new QuantLib::IntegralCdsEngine (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::~CIntegralCdsEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppIntegralCdsEngine != NULL)
    {
	    delete _ppIntegralCdsEngine;
        _ppIntegralCdsEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::!CIntegralCdsEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppIntegralCdsEngine != NULL)
    {
	    delete _ppIntegralCdsEngine;
    }
}
QuantLib::IntegralCdsEngine& Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::GetReference ()
{
    if (_ppIntegralCdsEngine == NULL) throw gcnew NativeNullException ();
	return **_ppIntegralCdsEngine;
}
boost::shared_ptr<QuantLib::IntegralCdsEngine>& Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::GetShared ()
{
    if (_ppIntegralCdsEngine == NULL) throw gcnew NativeNullException ();
	return *_ppIntegralCdsEngine;
}
QuantLib::IntegralCdsEngine* Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::GetPointer ()
{
    if (_ppIntegralCdsEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppIntegralCdsEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::IntegralCdsEngine>& Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::GetHandle ()
{
	if (_phIntegralCdsEngine == NULL)
	{
		_phIntegralCdsEngine = new Handle<QuantLib::IntegralCdsEngine> (*_ppIntegralCdsEngine);
	}
	return *_phIntegralCdsEngine;
}
#endif
bool Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::HasNative () 
{
	return (_ppIntegralCdsEngine != NULL);
}

Cephei::QL::Pricingengines::Credit::IIntegralCdsEngine^ Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine::Calculate::get ()
{
    try
    {
    	(*_ppIntegralCdsEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Credit::IIntegralCdsEngine^ Cephei::QL::Pricingengines::Credit::CIntegralCdsEngine_Factory::Create (Cephei::QL::Times::IPeriod^ integrationStep, Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ probability, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeSettlementDateFlows)
{
    return gcnew CIntegralCdsEngine ( integrationStep,  probability,  recoveryRate,  discountCurve,  includeSettlementDateFlows);
}
